package com.jltx.xq.coroutinesapp

import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit


abstract class BaseRetrofitServer {
    companion object {
        private const val TIME_OUT = 5
    }

    private val client: OkHttpClient get() {
        val builder = OkHttpClient.Builder()

            .connectTimeout(TIME_OUT.toLong(), TimeUnit.SECONDS)

        buildClient(builder)

        return builder.build()
    }

    abstract fun buildClient(builder: OkHttpClient.Builder)

    fun <S> createService(serviceClass: Class<S>, baseUrl: String) : S {
        return Retrofit.Builder()
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(baseUrl)
            .build().create(serviceClass)
    }
}